// 服务层
const userController = require("../controller/userController")
// 导入自定义错误
const {ParameterError, ValidationError} = require("../utils/customError")
const md5 = require("md5");
const {validateFunc} = require("../utils/tools");

// 检查查询对象是否为空 如果为空则抛出错误
exports.loginUser = async (loginInfo) => {
    if (!loginInfo) {
        throw new ParameterError("参数错误")
    }

    if (!loginInfo.loginId) {
        throw new ParameterError("请输入登录账号")
    }

    if (!loginInfo.loginPwd) {
        throw new ParameterError("请输入登录密码")
    }

    if (loginInfo.role !== 0 && !loginInfo.role) {
        throw new ParameterError("请传入角色ID")
    }

    loginInfo.loginPwd = md5(loginInfo.loginPwd)


    return userController.findOneUser(loginInfo)
}


// 注册用户
exports.registerUser = async (userInfo) => {
    const userRule = {
        loginId: {
            presence: {
                // 是否为空
                allowEmpty: false
            },
            type: "string"
        },
        loginPwd: {
            presence: {
                // 是否为空
                allowEmpty: false
            },
            type: "string"
        },
        role: {
            presence: {
                allowEmpty: false
            },
            type: "integer"
        },
        nickname: {
            presence: {
                // 是否为空
                allowEmpty: false
            },
            type: "string"
        },
    }

    const result = userInfo.loginId && await userController.findUserByLoginId(userInfo.loginId);
    if (result) {
        // 通过验证后需要验证用户名是否已经存在
        throw new ValidationError("用户名已存在")
    }
    try {
        await validateFunc(userRule, userInfo);
        // 密码需要MD5加密
        userInfo.loginPwd = md5(userInfo.loginPwd)
        const res = await userController.createUser(userInfo);
        res.loginPwd = "null"
        return res
    } catch (e) {
        throw new ValidationError("数据验证失败")
    }
}


// 通过ID获取用户
exports.getIsLoginById = async (loginId) => {
    const result = await userController.findUserByLoginId(loginId)
    return result !== null
}

// 分页获取用户
exports.getUserByPage = (query)=>{
    return userController.findUserByPage(query)
}


// 修改用户信息
exports.editUserInfoById = async (id, newUserInfo) => {
    console.log(newUserInfo)
    if (newUserInfo.loginId) {
        const result = await userController.findUserByLoginId(newUserInfo.loginId)
        const resp = await userController.findOneUser({
            _id:id,
            loginId:newUserInfo.loginId
        })
        if (result && !resp) {
            throw new ValidationError("用户名已存在")
        }
    }
    if (newUserInfo.newLoginPwd) {
        if(!newUserInfo.oldPwd) {
            throw new ValidationError("请填写旧密码")
        }
        // 如果有 则是修改账号密码
        const result = await userController.findOneUser({
            _id:id,
            loginPwd: md5(newUserInfo.oldPwd)
        })
        console.log(md5(newUserInfo.oldPwd),newUserInfo.oldPwd)
        if (!result) {
            // 如果查不到 说明密码不正确
            throw new ValidationError("旧密码不正确")
        }
        newUserInfo.loginPwd = md5(newUserInfo.newLoginPwd)
    }else{
        delete newUserInfo.loginPwd
    }

    return userController.updateUserInfoById(id, newUserInfo)
}

// 注销用户
exports.deleteUserInfoById = (id)=>{
    return userController.deleteUserById(id)
}
